<?php
/**
 * Created by PhpStorm.
 * User: ZZF
 * Date: 2017/8/21
 * Time: 15:30
 */

namespace app\admin\controller;

class UploadifyController extends CommonController{

    public function upload(){
        $func = input('param.func');
        $path = input('param.path','temp');
        $type = input('param.type');
        $info = array(
            'num'=> input('param.num'),
            'title' => '',
            'upload' =>Url('Admin/Uploadify/imageUp',array('savepath'=>$path,'pictitle'=>'banner','dir'=>'images')),
            'size' => '4',
            'type' =>'jpg,png,gif,jpeg',
            'input' => input('param.input'),
            'func' => empty($func) ? 'undefined' : $func,
        );
        if($type){
            $info['type'] = $type;
        }
        $this->assign('info',$info);
        return $this->fetch('upload');
    }

    /*
              删除上传的图片
     */
    public function delupload(){
        $action=isset($_GET['action']) ? $_GET['action'] : null;
        $filename= isset($_GET['filename']) ? $_GET['filename'] : null;
        $filename= str_replace('../','',$filename);
        $filename= trim($filename,'.');
        $filename= trim($filename,'/');
        if($action=='del' && !empty($filename)){
            $size = getimagesize($filename);
            $filetype = explode('/',$size['mime']);
            if($filetype[0]!='image'){
                return false;
                exit;
            }
            unlink($filename);
            // unlink(handleImage($filename)['smallFile']);
            exit;
        }
    }

    /**
     * @function imageUp
     */
    public function imageUp()
    {
        // 上传图片框中的描述表单名称，
        $files = request()->file('file');
        $fileName = '';
        $imageUrl = '';
        $original = '';
        //移动到框架应用根目录/public/uploads/目录下
        $info = $files->move(ROOT_PATH.'public'.DS.'uploads');
        if($info){
            //成功上传后 获取上传信息
            //输出 jpg
            $imageUrl = '/public/uploads/'.$info->getSavename();
            //输出
            $fileName = $info->getFilename();
        }else{
        //上传失败获取错误信息
            $original = $files->getError();
        }

        $state = 'SUCCESS';
        $return_data['url'] = $imageUrl;
        $return_data['title'] = $fileName;
        $return_data['original'] = '';
        $return_data['state'] = $state;
        return json_encode($return_data);
    }

}